<template>
	<view class="wrap">
		<view class="search"><u-search v-model="keywords" @custom="search" @search="search" @clear="clear"></u-search></view>
		<scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
			<u-cell-group class="list" :border="false">
				<u-swipe-action v-for="(item, index) in list" :key="index" :index="index" @click="optionsClick">
					<u-cell-item :arrow="true" @click="navTo(item.id, index)" style="border-bottom: 1px solid #ededed">
						<text slot="title">{{ item.taskTitle || item.id }}</text>
						<view slot="label" style="padding-bottom:20rpx">任务类型：{{ item.taskTypeName }}</view>
						<view slot="label" style="padding-bottom:20rpx">任务状态：{{ getStatusName(item.taskStatus) }}</view>
						<text slot="label">
							<u-button
								v-if="item.taskStatus == 4"
								type="primary"
								style="margin-top: 25rpx; width: 30%; margin-bottom: 4rpx ;margin-right:10rpx;"
								@click="navTo1(item)"
								:plain="true"
								:hairline="true"
							>
								编辑
							</u-button>
						</text>
					</u-cell-item>
				</u-swipe-action>
			</u-cell-group>
			<view class="loadmore" @click="loadMore"><u-loadmore :status="loadStatus"></u-loadmore></view>
		</scroll-view>
		<view class="btn-plus" @click="navTo('form')"><u-icon name="plus-circle-fill" size="90" color="#3d87ff"></u-icon></view>
	</view>
</template>
<script>
/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
export default {
	data() {
		return {
			keywords: '',
			query: {
				dataLimit: 0,
				pageNo: 1,
				pageSize: 20,
				orderBy: '',
				taskTypeCode: '',
				taskTitle: '',
				taskYear: new Date().getFullYear()
			},
			list: [],
			count: 0,
			loadStatus: 'loadmore',
			options: [{ text: '删除', style: { background: '#dd524d' } }]
		};
	},
	onLoad(op) {
		uni.setNavigationBarTitle({
			title: '任务部署'
		});
	},
	created() {
		this.loadList();
	},
	onShow() {
		if (uni.getStorageSync('refreshList') === true) {
			uni.removeStorageSync('refreshList');
			this.search('');
		}
	},
	methods: {
		// 根据任务状态返回中文名称
		getStatusName(statusCode) {
			switch(statusCode){
				case '0': return '删除'; break;
				case '1': return '草稿'; break;
				case '2': return '已发布'; break;
				case '3': return '任务部署审核通过'; break;
				case '4': return '任务部署审核驳回'; break;
				case '5': return '秘书长审核通过'; break;
				case '6': return '秘书长审核驳回'; break;
				case '7': return '已确认'; break;
				case '8': return '进行中'; break;
				case '9': return '已结束'; break;
				default: return '未知';
			}
		},
		loadMore() {
			this.loadStatus = 'loading';
			setTimeout(() => {
				this.query.pageNo += 1;
				this.loadList();
			}, 100);
		},
		loadList() {
			this.$u.api.taskInfo.list(this.query).then(res => {
				if (!res.list || res.list.length == 0) {
					this.loadStatus = 'nomore';
					return;
				}
				this.list = this.list.concat(res.list);
				this.count = res.count;
				this.query.pageNo = res.pageNo;
				this.query.pageSize = res.pageSize;
				this.loadStatus = 'loadmore';
			});
		},

		optionsClick(rowIndex, btnIndex) {
			if (btnIndex == 0) {
				let self = this;
				uni.showModal({
					title: '提示',
					content: '确认要删除该数据吗？',
					showCancel: true,
					success: function(res2) {
						if (res2.confirm) {
							let row = self.list[rowIndex];
							self.$u.api.testData.delete({ id: row.id }).then(res => {
								self.$u.toast(res.message);
								if (res.result == 'true') {
									self.list.splice(rowIndex, 1);
								}
							});
						}
					}
				});
			}
		},
		search(value) {
			this.list = [];
			this.query.pageNo = 0;
			this.query.taskTitle = value;
			this.loadList();
		},
		clear() {
			this.list = [];
			this.query.pageNo = 0;
			this.query.taskTitle = '';
			this.loadList();
		},
		navTo1(item) {
			uni.navigateTo({
				url: './taskInfoForm?id=' + item.id
			});
			uni.setStorageSync('add', 1);
		},
		navTo(url, index) {
			if (url == 'form') {
				uni.setStorageSync('add', 1);
				uni.navigateTo({
					url: './taskInfoForm'
				});
			} else {
				uni.navigateTo({
					url: './taskInfoForm?id=' + url
				});
				uni.setStorageSync('add', 2);
			}
		}
	}
};
</script>
<style lang="scss">
page {
	background-color: #f8f8f8;
}
.btn-plus {
	position: absolute;
	bottom: 50rpx;
	right: 50rpx;
	z-index: 1;
	opacity: 0.6;
}
.btn-plus:hover {
	opacity: 1;
}
// .u-cell {
// 	padding: 13px 25px !important;
// 	display: block !important;
// 	overflow: hidden;
// 	span{
// 		width: 80%!important;
// 	}
// }
</style>
